package com.banshuibao.bsb_android.fragment;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.activity.RegisterActivity;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.UserMessageModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;

/**
 * A simple {@link Fragment} subclass.
 */
public class OfficeMineFragment extends BaseFragment implements View.OnClickListener {

    private ImageView head_iv, head_tag;
    private TextView login, register, my_name, my_duty;
    private RelativeLayout no_login, counsel_list, my_wallet,
            buy_video, my_card, inform,
            setting, invite_member, download;
    private LinearLayout login_ok;
    private String vip;
    private String role;
    private TextView apply_text;
    private String status;
    private String headImg;
    private String name;
    private String phone;

    public OfficeMineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine_office, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initMenuView();
    }

    @Override
    public void onResume() {
        super.onResume();
        GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(getActivity()).readUser();
        if (!TextUtils.isEmpty(getLoginInfo.getCookieId())) {
            getUserPostTwo();
        }
    }

    private void getUserPostTwo() {
        HttpUtil.doPost(Constants.Url.USER_MESSAGE, null, new HttpResponse(getActivity(), UserMessageModel.class) {
            @Override
            public void onResult(Object obj) {
                UserMessageModel messageModel = (UserMessageModel) obj;
                UserMessageModel.Data data = messageModel.getData();
                headImg = data.getHeadImg();
                name = data.getName();
                phone = data.getPhone();
                //显示名称或电话号码
                if (!TextUtils.isEmpty(name) || !TextUtils.isEmpty(phone)) {
                    no_login.setVisibility(View.GONE);
                    login_ok.setVisibility(View.VISIBLE);
                    if (!TextUtils.isEmpty(name)) {
                        my_name.setText(name);
                    } else {
                        my_name.setText(phone);
                    }
                }
                if (!TextUtils.isEmpty(headImg)) {
                    ImageUtil.loadImageHead(getActivity(), headImg, head_iv);
                }
            }
        });
    }

    /**
     * 判断用户身份是否是咨询师
     */
    private boolean isRole() {
        return TextUtils.equals("4", MyApplication.applicationInstance.getLoginInfo().getRole());
    }

    private void initMenuView() {
        no_login = getView().findViewById(R.id.no_login);
        login_ok = getView().findViewById(R.id.login_ok);
        head_iv = getView().findViewById(R.id.head_iv);
        head_tag = getView().findViewById(R.id.head_tag);
        login = getView().findViewById(R.id.login);
        apply_text = getView().findViewById(R.id.apply_text);
        register = (TextView) getView().findViewById(R.id.register);
        my_name = getView().findViewById(R.id.name);
        my_name.getPaint().setFakeBoldText(true);
        my_duty = getView().findViewById(R.id.duty);
        counsel_list = getView().findViewById(R.id.counsel_list);
        my_wallet = getView().findViewById(R.id.my_wallet);
        buy_video = getView().findViewById(R.id.buy_video);
        my_card = getView().findViewById(R.id.my_card);
        inform = getView().findViewById(R.id.inform);
        setting = getView().findViewById(R.id.setting);
        invite_member = getView().findViewById(R.id.invite_members);
        download = getView().findViewById(R.id.download);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        counsel_list.setOnClickListener(this);
        my_wallet.setOnClickListener(this);
        head_tag.setOnClickListener(this);
        head_iv.setOnClickListener(this);
        setting.setOnClickListener(this);
        my_card.setOnClickListener(this);
        buy_video.setOnClickListener(this);
        my_name.setOnClickListener(this);
        inform.setOnClickListener(this);
        invite_member.setOnClickListener(this);
        download.setOnClickListener(this);
        //得到本地头像和名字
        GetLoginInfo loginInfo = GetLoginInfo.getLoginInfoInstance(getActivity()).readUser();
        String duty = loginInfo.getDuty();
        String position = loginInfo.getPosition();
        role = loginInfo.getRole();
        vip = loginInfo.getVip();

        //根据角色显示职责
        if (TextUtils.equals("2", role) || TextUtils.equals("3", role) || TextUtils.equals("4", role)) {
            if (!TextUtils.isEmpty(duty)) {
                my_duty.setText(duty);
            } else if (!TextUtils.isEmpty(position)) {
                my_duty.setText(position);
            }
        } else if (!TextUtils.isEmpty(position)) {
            my_duty.setText(position);
        }

        /**
         * 根据Vip判断显示
         */
        if (TextUtils.equals("0", vip)) {
            head_tag.setImageResource(R.drawable.vip);
            Drawable drawable = getResources().getDrawable(R.drawable.vip_point);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            my_name.setCompoundDrawables(null, null, drawable, null);
            my_name.setCompoundDrawablePadding(10);
        } else if (TextUtils.equals("1", vip)) {
            head_tag.setImageResource(R.drawable.to_vip);
            Drawable drawable = getResources().getDrawable(R.drawable.no_vip_point);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            my_name.setCompoundDrawables(null, null, drawable, null);
            my_name.setCompoundDrawablePadding(10);
        }
    }

    @Override
    public void onClick(View view) {
        GetLoginInfo loginInfo = MyApplication.applicationInstance.getLoginInfo();
        switch (view.getId()) {
            case R.id.head_tag:
                IntentManager.goToVipActivity(getActivity(), vip);
                break;
            case R.id.head_iv:
                if (!TextUtils.isEmpty(loginInfo.getCookieId())) {
                    IntentManager.goPersonalDataActivity(getActivity(), name, headImg, phone);
                } else {
                    IntentManager.goLoginActivity(getActivity());
                }
                break;
            case R.id.name:
                if (!TextUtils.isEmpty(loginInfo.getCookieId())) {
                    IntentManager.goPersonalDataActivity(getActivity(), name, headImg, phone);
                } else {
                    IntentManager.goLoginActivity(getActivity());
                }
                break;
            case R.id.login:
                IntentManager.goLoginActivity(getActivity());
                break;
            case R.id.register:
                Intent intent1 = new Intent(getActivity(), RegisterActivity.class);
                startActivity(intent1);
                break;
            case R.id.counsel_list:
                IntentManager.isJurisdiction(getActivity());
//                XToast.showToast(SlidingActivity.this, "我的清单");
                break;
            case R.id.my_wallet:
                IntentManager.goBalanceActivity(getActivity());
//                XToast.showToast(SlidingActivity.this, "我的钱包");
                break;
            case R.id.setting:
                IntentManager.goSettingActivity(getActivity());
                break;
            case R.id.my_card:
                if (vip.equals("1")) {
                    IntentManager.IsVip(getActivity());
                } else {
                    IntentManager.goMyVipCardActivity(getActivity());
                }
                break;
            case R.id.buy_video:
                IntentManager.goBuyMediaListActivity(getActivity());
                break;
            case R.id.inform:
                IntentManager.goSystemInformationActivity(getActivity());
                break;
            case R.id.invite_members:
                IntentManager.goInviteMembersActivity(getActivity());
                break;
            case R.id.download:
                IntentManager.goDownloadListActivity(getActivity());
                break;

        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        // super.onSaveInstanceState(outState);
    }
}
